package com.solarStruggle.graphics

import java.awt.{Color, Graphics, Point}
import scala.swing.{Panel}
import util.{Random}

class StarField(width: Int, height: Int, density: Int, fill: Color, colorDesc: RandomColorDescriptor)
extends ImageGenerator(width, height) {

  private case class Star(pos: (Int, Int), size: Int, color: Color)

  def createImage(): BufferedImage = {
    
    val r = new Random()

    def numStars = density * (width / 100) * density * (height / 100)
    
    def genStar(): Star = Star((r.nextInt(width), r.nextInt(height)), r.nextInt(5), colorDesc.getColor(r))
    
    var stars: Seq[Star] = for(i <- 0 until numStars)
      yield genStar()
    
    val image = new BufferedImage(width, height)
    val g = image.getGraphics

	  g.setColor(fill)
 		g.fillRect(0, 0, width, height)

    for(star <- stars) {
      g.setColor(star.color)
      g.fillOval(star.pos._1, star.pos._2, star.size, star.size)
  	}

    image
  }
}
